//
//  ViewController.swift
//  02-闭包的返回值
//
//  Created by 李南江 on 15/9/5.
//  Copyright © 2015年 xiaomage. All rights reserved.
//

import UIKit

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        /*
        let sc = UIScrollView(frame: CGRect(x: 0, y: 100, width: 375, height: 44))
        sc.backgroundColor = UIColor.redColor()
        let count = 15
        let width = 50
        for i in 0..<count{
            let label = UILabel()
            label.backgroundColor = UIColor.greenColor()
            label.textColor = UIColor.darkGrayColor()
            label.font = UIFont.systemFontOfSize(17)
            label.text = "text\(i)"
            label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)
            sc.addSubview(label)
        }
        
        sc.contentSize = CGSize(width: count * width, height: 44)
        view.addSubview(sc)
        */
        
        let sc = createScrollView({ () -> Int in
            return 15
            }) { (index) -> UILabel in
                let width = 50
                let label = UILabel()
                label.backgroundColor = UIColor.greenColor()
                label.textColor = UIColor.darkGrayColor()
                label.font = UIFont.systemFontOfSize(17)
                label.text = "text\(index)"
                label.frame = CGRect(x: index * width, y: 0, width: width, height: 44)
                return label
        }
        view.addSubview(sc)
        
    }
    
    func createScrollView(labelCount: ()->Int, labelWithIndex: (index:Int)->UILabel) -> UIScrollView{
        // 1.创建UIScrollView
        let sc = UIScrollView(frame: CGRect(x: 0, y: 100, width: 375, height: 44))
        
        let count = labelCount()
//        let width = 50
        
        // 2.遍历创建UILabel
        for i in 0..<count{
            /*
            let label = UILabel()
            label.backgroundColor = UIColor.greenColor()
            label.textColor = UIColor.darkGrayColor()
            label.font = UIFont.systemFontOfSize(17)
            label.text = "text\(i)"
            label.frame = CGRect(x: i * width, y: 0, width: width, height: 44)
            sc.addSubview(label)
            */
            let label = labelWithIndex(index: i)
            sc.addSubview(label)
            sc.contentSize = CGSize(width: CGFloat(count) * label.bounds.width, height: 44)
        }
        
        // 返回UIScrollView
        return sc
    }
}

